package pers.brozen.anka.service;

import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import pers.brozen.anka.apis.params.category.AddCategoryParam;
import pers.brozen.anka.apis.params.category.DeleteCategoryParam;
import pers.brozen.anka.apis.params.gallery.GetGalleryDetailParam;
import pers.brozen.anka.apis.params.gallery.GetGalleryPageParam;
import pers.brozen.anka.apis.vos.CategoryVo;
import pers.brozen.anka.apis.vos.GalleryDetailVo;
import pers.brozen.anka.apis.vos.GalleryImageVo;
import pers.brozen.anka.apis.vos.GalleryListItemVo;
import pers.brozen.anka.apis.vos.PageVo;
import pers.brozen.anka.dao.entity.Category;
import pers.brozen.anka.dao.entity.Gallery;
import pers.brozen.anka.dao.entity.GalleryImage;
import pers.brozen.anka.dao.repository.CategoryRepository;
import pers.brozen.anka.dao.repository.GalleryImageRepository;
import pers.brozen.anka.dao.repository.GalleryRepository;
import pers.brozen.anka.support.web.response.ErrorCodes;
import pers.brozen.anka.support.web.response.ResponseException;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Brozen
 * @since 2024-05-28
 */
@Service
public class GalleryService {

    @Resource
    private GalleryRepository galleryRepo;

    @Resource
    private GalleryImageRepository galleryImageRepo;


    public PageVo<GalleryListItemVo> getPage(GetGalleryPageParam param) {
        Sort querySort = Sort.by(Sort.Direction.DESC, "uploadedAt");
        PageRequest queryPage = PageRequest.of(param.getPage(), param.getSize(), querySort);
        Page<Gallery> page = galleryRepo.findAll(queryPage);

        PageVo<GalleryListItemVo> pageVo = new PageVo<>();
        pageVo.setPageCount(page.getTotalPages());
        pageVo.setItemCount((int) page.getTotalElements());
        pageVo.setItems(page.get().map(gallery -> {
            GalleryListItemVo vo = new GalleryListItemVo();
            vo.setId(gallery.getId());
            vo.setTitle(gallery.getTitle());
            vo.setCoverImageUrl(gallery.getCoverImageUrl());
            vo.setNeedWatchAd(gallery.getNeedWatchAd());
            return vo;
        }).collect(Collectors.toList()));
        return pageVo;
    }



    @Cacheable(cacheNames = "AnkaCache", key = "#param.getId()")
    public GalleryDetailVo getGalleryDetail(GetGalleryDetailParam param) {
        return galleryRepo.findById(param.getId()).map(gallery -> {
            GalleryDetailVo vo = new GalleryDetailVo();
            vo.setId(gallery.getId());
            vo.setTitle(gallery.getTitle());
            vo.setImageCount(gallery.getImageCount());
            vo.setCoverImageUrl(gallery.getCoverImageUrl());
            vo.setNeedWatchAd(gallery.getNeedWatchAd());
            if (!gallery.getNeedWatchAd()) {
                List<GalleryImage> images = galleryImageRepo.findByGalleryId(gallery.getId());
                images.sort(Comparator.comparing(GalleryImage::getSort));
                vo.setImages(images.stream().map(img -> {
                    GalleryImageVo imgVo = new GalleryImageVo();
                    imgVo.setId(img.getId());
                    imgVo.setWidth(img.getWidth());
                    imgVo.setHeight(img.getHeight());
                    imgVo.setUrl(img.getUrl());
                    return imgVo;
                }).collect(Collectors.toList()));
            }
            return vo;
        }).orElseThrow(ErrorCodes.NOT_FOUND::toException);
    }




}
